package com.welab.springcloud.demo.user.service;

import com.welab.springcloud.demo.dto.AuthRequest;
import com.welab.springcloud.demo.dto.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

@Slf4j
@RefreshScope
@Service
public class UserService {

    @Value("${system.config.token}")
    private String configToken;

    @Value("${system.config.password}")
    private String password;

    public User getUserById(Integer userId) {
        if (110 == userId) {
            //模拟不存在
            return null;
        }
        return User.builder().id(userId).userName("xiaomin").address("基金小镇").build();
    }

    public String auth(AuthRequest authRequest) {
        if (password.equalsIgnoreCase(authRequest.getPassword())) {
            return configToken;
        }
        return null;
    }

    public boolean parseToken(String token) {
        log.info("configToken:{},userToken:{}", configToken, token);
        return configToken.equalsIgnoreCase(token);
    }
}
